<?php
/**
 * Created by PhpStorm.
 * User: davin
 * Date: 14-10-19
 * Time: 下午9:25
 */


class MobileOrderService extends ServiceBase {


    public function getList($inputs = array(), $pageCount = null){
        //允许排序的字段名称数组
        $canOrderCols = array('id', 'status', 'updated_at', 'created_at');

        $order = $this->getSearchField($inputs, 'order');
        $reverse = $this->getSearchField($inputs, 'reverse', 0 );
        $page = $this->getSearchField($inputs, 'page', 1 );

        $member_id = $this->getSearchField($inputs,  'member_id', null );
        $status = $this->getSearchField($inputs,  'status', null );
        $created_at_start = $this->getSearchField($inputs,  'created_at_end', null );
        $created_at_end = $this->getSearchField($inputs,  'created_at_end', null );
        $updated_at_start = $this->getSearchField($inputs,  'updated_at_start', null );
        $updated_at_end = $this->getSearchField($inputs,  'updated_at_end', null );

        $entries = Order::orderby(in_array($order, $canOrderCols)?$order:'updated_at', $reverse == 0?'desc':'asc');

        if(!empty($created_at_start)){
            $created_at_start = strtotime($created_at_start);
            $created_at_start = date('Y-m-d', $created_at_start).' 00:00:00';

            $entries = $entries->where('created_at', '>=', $created_at_start);
        }

        if(!empty($created_at_end)){
            $created_at_end = strtotime($created_at_end);
            $created_at_end = date('Y-m-d', $created_at_end).' 23:59:59';

            $entries = $entries->where('created_at', '<=', $created_at_end);
        }

        if(!empty($updated_at_start)){
            $updated_at_start = strtotime($updated_at_start);
            $updated_at_start = date('Y-m-d', $updated_at_start).' 00:00:00';

            $entries = $entries->where('updated_at', '>=', $updated_at_start);
        }

        if(!empty($updated_at_end)){
            $updated_at_end = strtotime($updated_at_end);
            $updated_at_end = date('Y-m-d', $updated_at_end).' 23:59:59';

            $entries = $entries->where('updated_at', '<=', $updated_at_end);
        }

        if(!empty($member_id)){
            $entries = $entries->where('member_id', $member_id);
        }

        if(!empty($status)){
            $entries = $entries->where('status', $status);
        }

        $pageCount = is_numeric($pageCount) ? $pageCount : Config::get('app.pagenate_num');
        $entries = $entries->paginate($pageCount)
            ->addQuery('order',$order)
            ->addQuery('reverse',$reverse)

            ->addQuery('created_at_start',$created_at_start)
            ->addQuery('created_at_end',$created_at_end)
            ->addQuery('updated_at_start',$updated_at_start)
            ->addQuery('updated_at_end',$updated_at_end)
            ->addQuery('member_id',$member_id)
            ->addQuery('status',$status);

        $links = array(
            "order"=>$order,
            "reverse"=>$reverse,
            "page"=>$page,
            'created_at_start' => $created_at_start,
            'created_at_end' => $created_at_end,
            'updated_at_start' => $updated_at_start,
            'updated_at_end' => $updated_at_end,
            'member_id' => $member_id,
            'status' => $status,
        );

        return array(
            'entries'=>$entries,
            'links'=>$links,
        );

    }

    public function createOrder($member, $foodIds, $counts, $totalPrice){
        
        $order = $member->createOrder($foodIds, array_values($counts), $totalPrice);
        if(!$order){
            $this->errors()->merge($member->errors());
            return false;
        }
        return $order;
    }

    public function deleteOrder($order){
        if(!$order->delete()){
            $this->errors()->merge($order->errors());
            return false;
        }
        return true;
    }

    public function getLastOrder($member_id){
        $order = Order::where('member_id', $member_id)->orderBy('updated_at', 'desc')->first();
        if(!$order){
            $this->errors()->add('error', '您没有任何订单记录');
            return false;
        }
        return $order;
    }

    private function getSearchField($inputs, $key, $default = null){
        return isset($inputs[$key]) ? $inputs[$key] : Input::get($key, $default);
    }

}

